﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Diagnostics;
using System.IO;

namespace BingoServerCommon.Common
{
    public class CsvMgr
    {

        //! 读取CSV
        public static List<Dictionary<string, string>> ReadCsv(string filepath)
        {
            List<Dictionary<string, string>> table = new List<Dictionary<string, string>>();
            string txt = "";

            if (!File.Exists(filepath))
            {
                Console.WriteLine("读取CSV文件失败 未找到文件："+filepath);
                return table;
            }
            FileStream fs = File.Open(filepath, FileMode.Open);
            byte[] bytes = new byte[1024 * 80];
            int len = fs.Read(bytes, 0, bytes.Length);
            txt = Encoding.UTF8.GetString(bytes, 0, len);


            string[] lines = txt.Split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            string[] head = lines[0].Split(',');
            int cnt = head.Length;


            for (int i = 1; i < lines.Length; i++)
            {
                if (string.IsNullOrEmpty(lines[i]) || lines[i][0] == ',')
                    continue;

                try
                {
                    string[] row = lines[i].Split(',');
                    Dictionary<string, string> dict = new Dictionary<string, string>();
                    for (int j = 0; j < cnt; j++)
                    {
                        dict.Add(head[j], row[j]);
                    }
                    table.Add(dict);
                }
                catch { }
            }

            return table;
        }

        static private string[] GetRow(string line, int cnt)
        {
            line.Replace("\"\"", "\"");
            string[] strs = line.Split(',');
            if (strs.Length == cnt)
                return strs;

            List<string> lst = new List<string>();
            int n = 0, begin = 0;
            bool flag = false;

            for (int i = 0; i < strs.Length; i++)
            {
                if (strs[i].IndexOf("\"") == -1
                    || (!flag && strs[i][0] != '\"'))
                {
                    lst.Add(strs[i]);
                    continue;
                }

                n = 0;
                foreach (char ch in strs[i])
                {
                    if (ch == '\"')
                        n++;
                }
                if (n % 2 == 0)
                {
                    lst.Add(strs[i]);
                    continue;
                }

                flag = true;
                begin = i;
                i++;
                for (i = begin + 1; i < strs.Length; i++)
                {
                    foreach (char ch in strs[i])
                    {
                        if (ch == '\"')
                            n++;
                    }

                    if (strs[i][strs[i].Length - 1] == '\"' && n % 2 == 0)
                    {
                        StringBuilder sb = new StringBuilder();
                        for (; begin <= i; begin++)
                        {
                            sb.Append(strs[begin]);
                            if (begin != i)
                                sb.Append(",");
                        }
                        lst.Add(sb.ToString());
                        break;
                    }
                }
            }
            return lst.ToArray();
        }
    }
}
